package com.example.huadi1.service.Imp;

import com.example.huadi1.dto.Demand;
import com.example.huadi1.mapper.DemandMapper;
import com.example.huadi1.service.DepartmentService;
import com.example.huadi1.utils.AjaxResult;
import com.example.huadi1.utils.PageUtils;
import com.example.huadi1.utils.Request.SinglePageRequest;
import com.example.huadi1.utils.Request.PageRequest;
import com.example.huadi1.utils.Request.IntList;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DepartmentServiceImp implements DepartmentService {
    @Autowired
    private DemandMapper demandMapper;
    public AjaxResult getDemandById(PageRequest pageRequest){
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        Integer DepartmentId= pageRequest.getDepartmentId();
        List<Demand> demands=demandMapper.selectDemandsByDepartmentId(DepartmentId);
        PageInfo<Demand> pageAll = PageInfo.of(demands);
        PageUtils pageUtils = new PageUtils(pageAll);
        AjaxResult result=AjaxResult.success(pageUtils);
        return result;
    }
    public AjaxResult confirmById(IntList intList){
        for(int i=0;i<intList.getInts().size();i++){
            demandMapper.confirmDemand(intList.getInts().get(i));
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getAllDemand(SinglePageRequest singlePageRequest) {
        List<Demand> allDemand = demandMapper.getAllDemand();
        List<Integer> departmentIds=new ArrayList<>();
        for(Demand demand:allDemand){
            if(!departmentIds.contains(demand.getDepartmentId()))
                departmentIds.add(demand.getDepartmentId());
        }
        List<Map> result = new ArrayList<>();
        for (Integer item:departmentIds) {
            Map temp=new HashMap();
            temp.put("departmentId",item);
            result.add(temp);
        }




        return   AjaxResult.success(result);
    }

    public AjaxResult rejectDemandById(IntList intList){
        for(int i=0;i<intList.getInts().size();i++){
            demandMapper.rejectDemandById(intList.getInts().get(i));
        }
        return AjaxResult.success();
    }
    public AjaxResult acceptDemandById(IntList intList){
        for(int i=0;i<intList.getInts().size();i++){
            demandMapper.acceptDemandById(intList.getInts().get(i));
        }
        return AjaxResult.success();
    }
    public AjaxResult initDemand(){
        demandMapper.initDemand();
        return AjaxResult.success();
    }
}
